#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
//设置显示风速
void Set_Display_FanXiang(IT_AIR_FANXIANG FanXiang);
/* widgets:
Layer_KT_FanXiangSet
Background_KT_FanXiangSet
PopupButton_FanSetBack
Container_FanVertical_On
PopupButton_FanVerticalRight_On
PopupButton_FanVerticalMiddle_On
PopupButton_FanVerticalLeft_On
PopupButton_FanVerticalAuto_On
Container_FanLevel_On
PopupButton_FanLevelDown_On
PopupButton_FanLevelUp_On
PopupButton_FanLevelAuto_On
Container_FanXiangSet_Off
Animation_FanVerticalRight_Off
PopupButton_FanVerticalRight_Off
PopupButton_FanVerticalMiddle_Off
PopupButton105
Animation_FanVerticalLeft_Off
PopupButton_FanVerticalLeft_Off
PopupButton104
PopupButton103
Animation_FanVerticalAuto_Off
PopupButton_FanVerticalAuto_Off
PopupButton102
PopupButton101
PopupButton100
Animation_FanLevelDown_Off
PopupButton_FanLevelDown_Off
PopupButton99
Animation_FanLevelUp_Off
PopupButton_FanLevelUp_Off
PopupButton98
PopupButton97
Animation_FanLevelAuto_Off
PopupButton_FanLevelAuto_Off
PopupButton96
PopupButton95
PopupButton94
*/
static ITUBackground* Background_KT_FanXiangSet; 
static ITUAnimation* Animation_FanLevelAuto_Off;
static ITUAnimation* Animation_FanLevelUp_Off;
static ITUAnimation* Animation_FanLevelDown_Off;
static ITUAnimation* Animation_FanVerticalAuto_Off;
static ITUAnimation* Animation_FanVerticalLeft_Off;
static ITUAnimation* Animation_FanVerticalRight_Off;
static ITUContainer* Container_FanLevel_On;
static ITUContainer* Container_FanVertical_On;
static ITUPopupButton* PopupButton_FanLevelAuto_On;
static ITUPopupButton* PopupButton_FanLevelUp_On;
static ITUPopupButton* PopupButton_FanLevelDown_On;
static ITUPopupButton* PopupButton_FanVerticalAuto_On;
static ITUPopupButton* PopupButton_FanVerticalLeft_On;
static ITUPopupButton* PopupButton_FanVerticalRight_On;
IT_AIR_FANXIANG FanXiang_Temp;
static uint8_t DwellTime = 0;
bool KTFanXiangSetOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_KT_FanXiangSet)
	{
		Background_KT_FanXiangSet = ituSceneFindWidget(&theScene, "Background_KT_FanXiangSet");
		assert(Background_KT_FanXiangSet);
		Container_FanLevel_On = ituSceneFindWidget(&theScene, "Container_FanLevel_On");
		assert(Container_FanLevel_On);
		Container_FanVertical_On = ituSceneFindWidget(&theScene, "Container_FanVertical_On");
		assert(Container_FanVertical_On);
		PopupButton_FanLevelAuto_On = ituSceneFindWidget(&theScene, "PopupButton_FanLevelAuto_On");
		assert(PopupButton_FanLevelAuto_On);
		PopupButton_FanLevelUp_On = ituSceneFindWidget(&theScene, "PopupButton_FanLevelUp_On");
		assert(PopupButton_FanLevelUp_On);
		PopupButton_FanLevelDown_On = ituSceneFindWidget(&theScene, "PopupButton_FanLevelDown_On");
		assert(PopupButton_FanLevelDown_On);
		PopupButton_FanVerticalAuto_On = ituSceneFindWidget(&theScene, "PopupButton_FanVerticalAuto_On");
		assert(PopupButton_FanVerticalAuto_On);
		PopupButton_FanVerticalLeft_On = ituSceneFindWidget(&theScene, "PopupButton_FanVerticalLeft_On");
		assert(PopupButton_FanVerticalLeft_On);
		PopupButton_FanVerticalRight_On = ituSceneFindWidget(&theScene, "PopupButton_FanVerticalRight_On");
		assert(PopupButton_FanVerticalRight_On);
		Animation_FanLevelAuto_Off = ituSceneFindWidget(&theScene, "Animation_FanLevelAuto_Off");
		assert(Animation_FanLevelAuto_Off);
		Animation_FanLevelUp_Off = ituSceneFindWidget(&theScene, "Animation_FanLevelUp_Off");
		assert(Animation_FanLevelUp_Off);
		Animation_FanLevelDown_Off = ituSceneFindWidget(&theScene, "Animation_FanLevelDown_Off");
		assert(Animation_FanLevelDown_Off);
		Animation_FanVerticalAuto_Off = ituSceneFindWidget(&theScene, "Animation_FanVerticalAuto_Off");
		assert(Animation_FanVerticalAuto_Off);
		Animation_FanVerticalLeft_Off = ituSceneFindWidget(&theScene, "Animation_FanVerticalLeft_Off");
		assert(Animation_FanVerticalLeft_Off);
		Animation_FanVerticalRight_Off = ituSceneFindWidget(&theScene, "Animation_FanVerticalRight_Off");
		assert(Animation_FanVerticalRight_Off);
	}
	//进入页面重置动画
	ituAnimationReset(Animation_FanLevelAuto_Off);
	ituAnimationReset(Animation_FanLevelUp_Off);
	ituAnimationReset(Animation_FanLevelDown_Off);
	ituAnimationReset(Animation_FanVerticalAuto_Off);
	ituAnimationReset(Animation_FanVerticalLeft_Off);
	ituAnimationReset(Animation_FanVerticalRight_Off);

	ituAnimationPlay(Animation_FanLevelAuto_Off, 0);
	ituAnimationPlay(Animation_FanLevelUp_Off, 0);
	ituAnimationPlay(Animation_FanLevelDown_Off, 0);
	ituAnimationPlay(Animation_FanVerticalAuto_Off, 0);
	ituAnimationPlay(Animation_FanVerticalLeft_Off, 0);
	ituAnimationPlay(Animation_FanVerticalRight_Off, 0);

	ituWidgetSetVisible(Container_FanLevel_On, false);
	ituWidgetSetVisible(Container_FanVertical_On, false);

	Set_Display_FanXiang(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.FanXiang);
	Set_Rotate_Block(120);//旋转步进
    return true;
}

bool KTFanXiangSetOnLeave(ITUWidget* widget, char* param)
{
	if (FanXiang_Temp.vol != TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.FanXiang.vol)//状态有改变
	{
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.FanXiang.vol = FanXiang_Temp.vol;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.status = 0x58;//设置模式
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.FanXiang.vol = FanXiang_Temp.vol;
		printf("Moorgen INFO : Touch to set the air conditioner's FanXiang.\n");
		RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
	}
	DwellTime = 0;
    return true;
}

bool KTFanXiangSetOnTimer(ITUWidget* widget, char* param)
{
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
		Clear_User_Action();
	if (DwellTime < 250)
		DwellTime++;
	else
	{
		ITULayer *Layer;
		DwellTime = 0;
		Layer = ituSceneFindWidget(&theScene, "Layer_KongTiao");
		assert(Layer);
		ituLayerGoto(Layer);
	}
	return true;
}

bool PopupButton_KT_FanXiangExit_Press(ITUWidget* widget, char* param)
{
    return true;
}

bool PopupButton_FanSetVerticalRight_Press(ITUWidget* widget, char* param)
{
	FanXiang_Temp.Byte.FanVertical = 3; 
	DwellTime = 0;
    return true;
}

bool PopupButton_FanSetVerticalLeft_Press(ITUWidget* widget, char* param)
{
	FanXiang_Temp.Byte.FanVertical = 2;
	DwellTime = 0;
    return true;
}

bool PopupButton_FanSetVerticalAuto_Press(ITUWidget* widget, char* param)
{
	FanXiang_Temp.Byte.FanVertical = 1;
	DwellTime = 0;
    return true;
}

bool PopupButton_FanSetLevelDown_Press(ITUWidget* widget, char* param)
{
	FanXiang_Temp.Byte.FanLevel = 3;
	DwellTime = 0;
    return true;
}

bool PopupButton_FanSetLevelUp_Press(ITUWidget* widget, char* param)
{
	FanXiang_Temp.Byte.FanLevel = 2;
	DwellTime = 0;
    return true;
}

bool PopupButton_FanSetLevelAuto_Press(ITUWidget* widget, char* param)
{
	FanXiang_Temp.Byte.FanLevel = 1;
	DwellTime = 0;
    return true;
}
//设置显示风速
void Set_Display_FanXiang(IT_AIR_FANXIANG FanXiang)
{
	FanXiang_Temp = FanXiang;
	ituWidgetSetVisible(PopupButton_FanLevelAuto_On, false);
	ituWidgetSetVisible(PopupButton_FanLevelUp_On, false);
	ituWidgetSetVisible(PopupButton_FanLevelDown_On, false);
	ituWidgetSetVisible(PopupButton_FanVerticalAuto_On, false);
	ituWidgetSetVisible(PopupButton_FanVerticalLeft_On, false);
	ituWidgetSetVisible(PopupButton_FanVerticalRight_On, false);
	if (FanXiang_Temp.vol != 0)
	{
		if (FanXiang_Temp.Byte.FanLevel == 1)
		{
			//水平自动
			ituWidgetSetVisible(PopupButton_FanLevelAuto_On, true);
		}
		else if (FanXiang_Temp.Byte.FanLevel == 2)
		{
			//水平向下
			ituWidgetSetVisible(PopupButton_FanLevelUp_On, true);//和APP的UI反了，做更正
		}
		else if (FanXiang_Temp.Byte.FanLevel == 3)
		{
			//水平向上
			ituWidgetSetVisible(PopupButton_FanLevelDown_On, true);//和APP的UI反了，做更正
		}
		if (FanXiang_Temp.Byte.FanVertical == 1)
		{
			ituWidgetSetVisible(PopupButton_FanVerticalAuto_On, true);
			//垂直自动
		}
		else if (FanXiang_Temp.Byte.FanVertical == 2)
		{
			ituWidgetSetVisible(PopupButton_FanVerticalLeft_On, true);
			//垂直向左
		}
		else if (FanXiang_Temp.Byte.FanVertical == 3)
		{
			ituWidgetSetVisible(PopupButton_FanVerticalRight_On, true);
			//垂直向右
		}
	}
}
